<?php 
namespace app\api\controller\v1;

use think\Db;


//
class CdkeyController extends UserBaseController{



	//通过燃料激活
	function activebyamount(){
		$app_config = cmf_get_option('app_config');
		$price = $app_config['quant_startup_min'];
		if($price<=0) $price=100;
		if($this->amount<$price){
			return json(['error'=>1,'msg'=>$this->lang['AMOUNT_NO_ENOUGH']]);
		}
		$deposit = $app_config['deposit'];
		if($deposit<=0) $deposit=20;
		if($this->amount < $price + $deposit){
			return json(['error'=>1,'msg'=>$this->lang['DEPOSIT_NO_ENOUGH']]);
		}
		Db::startTrans();
		try {
			$log_id=Db::name('user_active_log')
				->insertGetId([
					'user_id'=>$this->user['id'],
					'cdkey_id' =>0,
					'info'     =>'使用燃油激活',
					'create_at'=>time()
				]);
			//扣除费用
			if($this->user['active_expire']<time()){
				$active_expire = time()+ $row['term'] * 24 * 3600;
			}else{
				$active_expire = $this->user['active_expire']+ $row['term'] * 24 * 3600;
			}
			$amount = $this->user['amount'] - $price;
			Db::name('user')->where('id',$this->user['id'])->update(['active_expire'=>$active_expire,'amount'=>$amount]);

			//写入流水
			Db::name('balance')
			->insert([
				'type'         =>$type,
				'user_id'      =>$user['id'],
				'amount'       =>0-$price,
				'end_amount'   =>$amount,
				'create_at'    =>time(),
				'remark'       =>'燃油激活',
				'table'        =>'user_active_log',
				'table_id'     =>$log_id
			]);
			Db::commit();
			return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);
		} catch (Exception $e) {
			Db::rollback();
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}
	}


	function active(){
		$keycode = input('param.keycode/s');
		if(empty($keycode)){
			return json(['error'=>1,'msg'=>$this->lang['CDKEY'].$this->lang['MUST_INPUT']]);
		}
		$row = Db::name('cdkey')->where('keys',$keycode)->find();
		if(empty($row) || $row['user_id']!= $this->user_id){
			return json(['error'=>1,'msg'=>$this->lang['CDKEY'].$this->lang['NO_EXISTS'].$this->lang['PLEASE'].$this->lang['BUY'],'data'=>$rows]);
		}
		Db::startTrans();
		try {
			Db::name('cdkey')->where('id',$row['id'])->update(['status'=>1]);
			$log_id=Db::name('cdkey_log')
				->insertGetId([
					'user_id'=>$this->user['id'],
					'cdkey_id' =>$row['id'],
					'info'     =>'激活',
					'ctime'=>time()
				]);
			Db::name('user_active_log')
				->insert([
					'user_id'=>$this->user['id'],
					'cdkey_id' =>$row['id'],
					'info'     =>'使用激活码激活',
					'create_at'=>time(),
				]);
			if($this->user['active_expire']<time()){
				$active_expire = time()+ $row['term'] * 24 * 3600;
			}else{
				$active_expire = $this->user['active_expire']+ $row['term'] * 24 * 3600;
			}
			Db::name('user')->where('id',$this->user['id'])->update(['active_expire'=>$active_expire]);
			Db::commit();
			return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);
		} catch (Exception $e) {
			Db::rollback();
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}
	}




	function category(){
		$rows = Db::name('cdkey_category')->where('delete_at',0)->order('price desc')->select()->toArray();
		$rows = $this->deal_category($rows);
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);

	}



	function my(){
		$rows = Db::name('cdkey')->where('user_id',$this->user_id)->order('create_at desc')->select()->toArray();
		$rets=[];
		foreach ($rows as $key => $value) {
			$row['id'] = $value['id'];
			$row['status'] = $value['status'];
			$row['status_title'] = $value['status']==1? $this->lang['USED'] : $this->lang['NO_USED'];
			$row['term'] = $value['term'];
			$row['keycode'] = $value['keys'];
			$row['create_at']  = date('Y-m-d H:i',$value['create_at']);
			$rets[]= $row;
		}
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rets]);

	}



	function purchase(){
		$data= input('param.');
		if(empty($data['paypwd'])){
			return json(['error'=>1,'msg'=>$this->lang['PAYPWD_INPUT_ALARM']]);
		}
		if($data['category_id']){
			return json(['error'=>1,'msg'=>$this->lang['CDKEY'].$this->lang['CATE_ID_ERROR']]);
		}
		$cate = Db::name('cdkey_category')->where(['id'=>$data['category_id'],'delete_at'=>0])->find();
		if($this->user['amount']< $cate['price']){
			return json(['error'=>1,'msg'=>$this->lang['AMOUNT_NO_ENOUGH']]);
		}
		//购买
		Db::startTrans();
		try {
			$cdkey =[
				'user_id'      =>$this->user_id,
				'status'       =>0,
				'term'         =>$cate['term'],
				'create_at'    =>time(),
				'category_id'  =>$cate['id'],
				'keys'         =>create_random_str(10,false)
			];
			//生产激活码
			Db::name('user')->where('id',$this->user['id'])->update(['amount'=>$this->user['amount']-$cate['price']]);
			Db::name('balance')
				->insert([
					'type'         =>$type,
					'user_id'      =>$user['id'],
					'amount'       =>0-$cate['price'],
					'end_amount'   =>$this->user['amount'] - $cate['price'],
					'create_at'    =>time(),
					'remark'       =>'购买激活码',
					'table'        =>$table,
					'table_id'     =>$table_id
				]);
		    Db::commit();
		    //分成
		    return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);	
		} catch (Exception $e) {
			Db::rollback();
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}
		
	}



	protected function deal_category($rows){
		$rets =[];
		foreach ($rows as $key => $value) {
			$row['id']        = $value['id'];
			$row['title']     = $value['title'];
			$row['term']      = $value['term'];
			$row['price']     = $value['price'];
			$rets[]= $row;
		}
		return $rets;
	}




}